package trabajoISW2.domain.implementations;

import java.util.LinkedList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;

import trabajoISW2.domain.contracts.IAuthor;
import trabajoISW2.domain.exceptions.WrongIdException;
import trabajoISW2.domain.exceptions.WrongNameException;

@Entity
public class Author implements IAuthor{
	
	@Id @GeneratedValue(strategy = GenerationType.AUTO)
	private int authorId;
	
	private String name;
	private String surname;
	
	@OneToMany (mappedBy = "author", cascade = {CascadeType.MERGE, CascadeType.REFRESH})
	private List<Book> books = new LinkedList<Book>();
	
	//Constructores
	public Author(String name, String surname, List<Book> books) {
		this.name = name;
		this.surname = surname;
		this.books = books;
	}
	
	public Author(){
		
	}

	public Author(int authorId,String name,String surname){
		this.authorId = authorId;
		this.name = name;
		this.surname = surname;
	}
	

	public Author(int authorId,String name,String surname,List<Book> lb){
		this.authorId = authorId;
		this.name = name;
		this.surname = surname;
		this.books = lb;
	}
	
	
	public Author(String name, String surname) {
		// TODO Auto-generated constructor stub
		this.name = name;
		this.surname = surname;
	}

	@Override
	public int getAuthorId() {
		return authorId;
	}
	
	@Override
	public void setAuthorId(int authorId) {
		if(authorId <0||authorId>Integer.MAX_VALUE)
			throw new WrongIdException();
		else
			this.authorId = authorId;
	}
	
	@Override
	public String getName() {
		return name;
	}
	
	@Override
	public void setName(String name) {
		if(name == null | name == "")
			throw new WrongNameException();
		else
			this.name = name;
	}
	
	@Override
	public String getSurname() {
		return surname;
	}
	
	@Override
	public void setSurname(String surname) {
		if(surname == null | surname == "")
			throw new WrongNameException();
		else
		this.surname = surname;
	}
	
	@Override
	public List<Book> getBooks() {
		return books;
	}
	
	@Override
	public void setBooks(List<Book> books) {
		if(books == null)
			throw new IllegalArgumentException();
		else
			this.books = books;
	}

	@Override
	public void addBook(Book book) {
		if(book == null)
			throw new IllegalArgumentException();
		else
			books.add(book);
	}

	@Override
	public void removeBook(Book book) {
		if(book == null)
			throw new IllegalArgumentException();
		else
			books.remove(book);
	}
	
	public boolean equals (Object o){
		boolean b = false;
		Author a = (Author) o;
		if (getAuthorId() == a.getAuthorId())
			b = true;
		return b;
	}
	
}
